package com.xiaozhichao.nosql.redis.pushsub;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Publisher implements Runnable {

    private final JedisPool jedisPool;
    private final String channel;

    public Publisher(JedisPool jedisPool,String channel) {
        this.jedisPool = jedisPool;
        this.channel = channel;
    }

    @Override
    public void run() {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        Jedis jedis = this.jedisPool.getResource();//连接池中取出一个连接
        while (true){
            try {
                String line = reader.readLine();
                if(!"quit".equals(line)){
                    jedis.publish(channel,line);
                }else{
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
